<?php

// $Id: vpage_edit_normal_wp.class.php 1570 2009-01-13 12:44:02Z ogrebg $


require_once($WSC->get_site_inc_dir().'modules/tangra_cms/_sys/tcms_admin_page_w_fctrl_ua.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/forms/_vpt/vpage_normal/vpage_normal_form_ctrl.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_dbc.class.php');


class VPage_Edit_Normal_WP extends TCMS_Admin_Page_W_FCtrl_UA {
	public function create_view($name = 'default') {
		$ret = new TCMS_Admin_Page_View($this, $name, 'modules/tangra_cms/pages/_vpage/_vpt/');

		return $ret;
	}


	protected function init_transit_vars() {
		$this->add_transit_var('category');
	}


	protected function create_form_ctrl() {
		$dbc = $this->get_dbc();
		$tvm = $this->get_tvm();

		$type = new TCMS_CI_Type_DBC();
		if (!$type->load_by_hid($dbc, 'normal_content_item')) {
			throw new Tangra_Module_Exception('Cannot load TCMS_CI_Type_DBC with hid = "normal_ci"');
		}

		$attributes = TCMS_CI_Type_Attr_DBC::select_all_for_ci_type($dbc, $type->get_id());

		$ret = new VPage_Normal_Form_Ctrl('vpage', $tvm, $dbc, 'obj_id', $attributes);

		return $ret;
	}


	protected function on_received_id($object_id) {
		$current_user = $this->get_user();
		$this->form_ctrl->set_permanent_external_param('user_id', $current_user->get_id());

		$this->export('vpage_id', $object_id);

		$category_id = $this->get_transit_var('category', true);
		if ($category_id) {
			$this->export('category_id', $category_id);
		}

		$vpage = new TCMS_VPage_DBC();
		$dbc = $this->get_dbc();
		if ($vpage->load_by_id($dbc, $object_id)) {
			$this->export('vpage', $vpage);
		} else {
			throw new Tangra_Module_Exception('Cannot load vpage with id = '.$object_id);
		}
	}


	protected function create_default_redir_view() {
		$object = $this->form_ctrl->get_object();
		$redir_view = $this->create_redirect_view($this->get_redir_on_success(), array('manage' => $object->get_id()));

		return $redir_view;
	}
}